#include <iostream>

#include <nemesis.dbms.Database.h>
#include <nemesis.dbms.Connection.h>
#include <nemesis.dbms.Statement.h>
#include <nemesis.dbms.Integer.h>
#include <nemesis.dbms.String.h>

#include <dbos_demo.defines.h>
#include <dbos_demo.phase.Populate.h>

using namespace std;
using namespace nemesis;
using namespace dbos_demo;

void phase::Populate::do_comment () const
   throw ()
{
   cout << "Crea las tablas usadas durante la prueba y las rellena con datos" << endl << endl;
   cout << "1.- Crea las tablas nemesis_db_table01, nemesis_db_table02, nemesis_db_table03" << endl;
   cout << "2.- Inserta " << MaxKey01 << " registros en nemesis_db_table01" << endl;
   cout << "3.- Inserta " << MaxKey02 << " registros en nemesis_db_table02" << endl;
   cout << "4.- Inserta " << MaxKey03 << " registros en nemesis_db_table03" << endl;  
}

void phase::Populate::do_apply (dbms::Connection& connection)
   throw (RuntimeException, dbms::DatabaseException)
{
   using namespace nemesis::dbms;

   Guard guard (connection);

   Database& database = connection.getDatabase ();

   ResultCode resultCode;
   
   Statement* create = NULL;
   Statement* insert = NULL;

   create = database.createStatement ("create01", "create table nemesis_db_table01 (key integer, ref02 integer, ref03 integer)");
   resultCode = connection.execute (create);
   if (resultCode.successful () == false)
      throw DatabaseException (resultCode, FILE_LOCATION);

   create = database.createStatement ("create02", "create table nemesis_db_table02 (key integer, text varchar2(16))");
   resultCode = connection.execute (create);
   if (resultCode.successful () == false)
      throw DatabaseException (resultCode, FILE_LOCATION);

   create = database.createStatement ("create03", "create table nemesis_db_table03 (key integer, text varchar2(16))");
   resultCode = connection.execute (create);
   if (resultCode.successful () == false)
      throw DatabaseException (resultCode, FILE_LOCATION);
   
   Integer key (true), table2 (true), table3 (true);
   String text (16, true);
   
   insert = database.createStatement ("insert01", "insert into nemesis_db_table01 (key, ref02, ref03) values (:k,:t02,:t03)");
   insert->bindInput ("Key", key);
   insert->bindInput ("T02", table2);
   insert->bindInput ("T03", table3);
   
   for (int i = 0; i < MaxKey01; i ++) {
      key = i;
      table2 = i % MaxKey02;
      table3 = 100 + i % MaxKey03;
      
      resultCode = connection.execute (insert);
      if (resultCode.successful () == false)
         cout << resultCode.asString () << endl;
   }      

   insert = database.createStatement ("insert02", "insert into nemesis_db_table02 (key, text) values (:k,:text)");
   insert->bindInput ("Key", key);
   insert->bindInput ("Text", text);
   
   for (int i = 0; i < MaxKey02; i ++) {
      key = i;
      text = nemesis::functions::asString (i);
            
      resultCode = connection.execute (insert);
      if (resultCode.successful () == false)
         cout << resultCode.asString () << endl;
   }      

   insert = database.createStatement ("insert03", "insert into nemesis_db_table03 (key, text) values (:k,:text)");
   insert->bindInput ("Key", key);
   insert->bindInput ("Text", text);
   
   for (int i = 0; i < MaxKey03; i ++) {
      key = 100 + i;
      text = nemesis::functions::asString (100 + i);
            
      resultCode = connection.execute (insert);
      if (resultCode.successful () == false)
         cout << resultCode.asString () << endl;
   }      
}

